package utils

import (
	"fmt"
	"mongofix/conf"
)

func GetColForGroupMemberByGroupId(groupId string) string {
	return fmt.Sprintf("group_member_%d", GetIdForGroupByGroupId(groupId)%conf.Tia_group_member_col_num)
}

func GetColForGroupByGroupId(groupId string) string {
	return fmt.Sprintf("group_%d", GetIdForGroupByGroupId(groupId)%conf.Tia_group_col_num)
}

func GetColForPostByGroupId(groupId string) string {
	return fmt.Sprintf("post_%d", GetIdForGroupByGroupId(groupId)%conf.Tia_post_col_num)
}

func GetDbForUserByUserId(userId string) int {
	_, mid := GetIdAndMidForUserByUserId(userId)
	return mid % conf.Tia_user_db_num
}

func GetColForUserByUserId(userId string) string {
	_, mid := GetIdAndMidForUserByUserId(userId)
	return GetColForUserByIndex((mid / conf.Tia_user_db_num) % conf.Tia_user_col_num)
}
func GetColForUserByIndex(index int) string {
	return fmt.Sprintf("user_%d", index)
}
func GetColForCommentOPByUserId(userId string) string {
	uid, _ := GetIdAndMidForUserByUserId(userId)
	return fmt.Sprintf("user_comment_op_%d", uid%conf.Tia_user_comment_op_col_num)
}
func GetColForUserPostOPByUserId(userId string) string {
	uid, _ := GetIdAndMidForUserByUserId(userId)
	return fmt.Sprintf("user_post_op_%d", uid%conf.Tia_user_post_op_col_num)
}
func GetColCommentByIndex(index int) string {
	return fmt.Sprintf("comment_%d", index)

}

func GetColUerPostOpByIndex(index int) string {
	return fmt.Sprintf("user_post_op_%d", index)

}

func GetIdAndPostIdForCommentByCommentId(commentId string) (int, int) {
	var cid, postId int
	fmt.Sscanf(commentId, conf.ID_FORMAT_COMMENT, &cid, &postId)
	return cid, postId
}
func GetIdAndGroupIdForPostByPostId(postId string) (int, int) {
	var pId, groupId int
	fmt.Sscanf(postId, conf.ID_FORMAT_POST, &pId, &groupId)
	return pId, groupId
}

func GetIdForGroupByGroupId(groupId string) int {
	var result int
	fmt.Sscanf(groupId, conf.ID_FROMAT_GROUP, &result)
	return result
}

func GetIdAndMidForUserByUserId(userId string) (int, int) {
	var id, mid int
	fmt.Sscanf(userId, conf.ID_FROMAT_USER, &id, &mid)
	return id, mid
}
